/**
 * Copyright (C) 2014  Universidade de Aveiro, DETI/IEETA, Bioinformatics Group - http://bioinformatics.ua.pt/
 *
 * This file is part of Dicoogle/dicoogle.
 *
 * Dicoogle/dicoogle is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Dicoogle/dicoogle is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Dicoogle.  If not, see <http://www.gnu.org/licenses/>.
 */

package pt.ua.dicoogle.rGUI.fileTransfer;

import java.awt.Desktop;
import java.awt.Image;
import java.awt.Toolkit;
import java.io.File;
import java.io.IOException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.swing.JOptionPane;
import pt.ua.dicoogle.Main;
import pt.ua.dicoogle.core.ClientSettings;
import pt.ua.dicoogle.rGUI.RFileBrowser.RemoteFile;

/**
 *
 * @author Samuel Campos <samuelcampos@ua.pt>
 */
@Deprecated
public class TransferStatus extends javax.swing.JFrame {

    private RemoteFile file;
    private String filePath;

    /** Creates new form TransferStatus */
    public TransferStatus(RemoteFile file) {
        initComponents();
        
        Image image = Toolkit.getDefaultToolkit().getImage(Thread.currentThread().getContextClassLoader().getResource("trayicon.gif"));
        this.setIconImage(image);

        this.file = file;
        jProgressBar.setMaximum((int) file.length());
        jProgressBar.setMinimum(0);
        jProgressBar.setValue(0);
        jProgressBar.setStringPainted(true);

        jButtonView.setEnabled(false);
        jLabelFileName.setText(file.getName());
        jLabelFileSize.setText(String.valueOf(file.length()));
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabelFileName = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabelFileSize = new javax.swing.JLabel();
        jProgressBar = new javax.swing.JProgressBar();
        jButtonView = new javax.swing.JButton();
        jLabelState = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabelProgress = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("File Transfer");

        jLabel1.setFont(new java.awt.Font("Lucida Grande", 1, 13));
        jLabel1.setText("File:");

        jLabelFileName.setText("<FileName>");

        jLabel2.setFont(new java.awt.Font("Lucida Grande", 1, 13));
        jLabel2.setText("Size (Bytes):");

        jLabelFileSize.setText("<FileSize>");

        jButtonView.setText("View");
        jButtonView.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonViewActionPerformed(evt);
            }
        });

        jLabelState.setFont(new java.awt.Font("Lucida Grande", 1, 13)); // NOI18N
        jLabelState.setText("Downloading the file from the server..");

        jLabel4.setFont(new java.awt.Font("Lucida Grande", 1, 13));
        jLabel4.setText("Progress:");

        jLabelProgress.setText("<Progress>");

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(36, 36, 36)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                        .add(jButtonView)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(jLabel4)
                            .add(jProgressBar, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 323, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                    .add(jLabelState)
                    .add(layout.createSequentialGroup()
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(jLabel2)
                            .add(jLabel1))
                        .add(18, 18, 18)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(jLabelFileName)
                            .add(jLabelFileSize)
                            .add(jLabelProgress))))
                .addContainerGap(41, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(26, 26, 26)
                .add(jLabelState)
                .add(18, 18, 18)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel1)
                    .add(jLabelFileName))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel2)
                    .add(jLabelFileSize))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 25, Short.MAX_VALUE)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel4)
                    .add(jLabelProgress))
                .add(7, 7, 7)
                .add(jProgressBar, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(jButtonView)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButtonViewActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonViewActionPerformed
        if (ClientSettings.getInstance().getExtV() == null
                || ClientSettings.getInstance().getExtV().equals("")) {

            try {
                Desktop.getDesktop().open(new File(filePath));

            } catch (IOException ex) {
                String folder = filePath.substring(0, filePath.lastIndexOf('/'));

                try {
                    Desktop.getDesktop().open(new File(folder));

                } catch (IOException ex1) {
                    JOptionPane.showMessageDialog(this, "Dicoogle can't open this file!", "Error opening the file", JOptionPane.ERROR_MESSAGE);
                }
            }
        } else {
            try {
                ProcessBuilder pb = new ProcessBuilder(ClientSettings.getInstance().getExtV(), filePath);
                pb.start();

            } catch (IOException ex) {

                String folder = filePath.substring(0, filePath.lastIndexOf('/'));

                try {
                    Desktop.getDesktop().open(new File(folder));

                } catch (IOException ex1) {
                    JOptionPane.showMessageDialog(this, "Dicoogle can't open this file!", "Error opening the file", JOptionPane.ERROR_MESSAGE);
                }
            }
        }
    }//GEN-LAST:event_jButtonViewActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonView;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabelFileName;
    private javax.swing.JLabel jLabelFileSize;
    private javax.swing.JLabel jLabelProgress;
    private javax.swing.JLabel jLabelState;
    private javax.swing.JProgressBar jProgressBar;
    // End of variables declaration//GEN-END:variables

    public void setFilePath(String filePath){
        this.filePath = filePath;
    }

    public void setTransferedBytes(long size){
        long perc = (size * 100 / file.length());

        jProgressBar.setValue((int) size);
        jProgressBar.setString(String.valueOf(perc) + "%");


        jLabelProgress.setText(String.valueOf(size));

        if(size == file.length()){
            jLabelState.setText("Download complete. Click 'View' button");

            if(this.filePath != null)
                jButtonView.setEnabled(true);
        }
    }

    public void errorInTransfer(String error){
        JOptionPane.showMessageDialog(this, error,
                    "Transfer Error", JOptionPane.ERROR_MESSAGE);

        //closes this window
        this.dispatchEvent(new java.awt.event.WindowEvent(this,
                java.awt.Event.WINDOW_DESTROY));
    }
}
